package sunshine.sun.cn.notifidemo;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    NotificationManager manager;
    Random random;
    TextView textView;
    Switch swithSD;
    String pathInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        findViewById(R.id.btSend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNotifi();
            }
        });
        textView = findViewById(R.id.tv);
        swithSD = findViewById(R.id.swithSD);

        textView.setText(DeviceUtils.GetDeviceIMEI(this));
        findViewById(R.id.btPlayVoice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playVoice();
            }
        });
        findViewById(R.id.btzd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vibrator();
            }
        });
        random = new Random();
        new Thread(new Runnable() {
            @Override
            public void run() {
                String path = DeviceUtils.getInternalPath("/防伪追溯");
                List<String> list = new ArrayList<>();
                list.add(path + "/Voice/" + "Information.wav");
                list.add(path + "/Voice/" + "Question.wav");
                list.add(path + "/Voice/" + "Warning.wav");
                list.add(path + "/Voice/" + "Error.wav");
                list.add(path + "/Voice/" + "scanFailure.mp3");
                list.add(path + "/Voice/" + "scanSuccess.mp3");
                List<Integer> lists = new ArrayList<>();
                lists.add(R.raw.information);
                lists.add(R.raw.question);
                lists.add(R.raw.warning);
                lists.add(R.raw.error);
                lists.add(R.raw.scanfailure);
                lists.add(R.raw.scansuccess);
                lists.add(R.raw.voice);
                SoundPoolUtil.addSoundsFromSD(list);
                SoundPoolUtil.addSoundsFromRaw(lists);
                SoundPoolUtil.initSoundPool(MainActivity.this.getApplicationContext());
            }
        }).start();
        swithSD.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SoundPoolUtil.setIsSDVoice(isChecked);
            }
        });
        pathInfo = DeviceUtils.getInternalPath("/防伪追溯") + "/Voice/" + "Information.wav";
        findViewById(R.id.btPlayMediaVoice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    MediaUtils.getInstance().playDataSource(pathInfo);
                    VibrateUtil.vibrate(MainActivity.this.getApplicationContext(), 500);
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.d(TAG, "播放出错：" + e.getMessage());
                }
            }
        });
    }

    boolean isVirating = false;

    private void vibrator() {
//        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
//
//        vibrator.vibrate(1000);  // 设置手机振动
//       boolean ishas= vibrator.hasVibrator();  // 判断手机硬件是否有振动器
        //vibrator.cancel();// 关闭振动
        isVirating = true;
        // 开启震动
        VibrateUtil.vibrate(this.getApplicationContext(), 500);
//        VibrateUtil.vibrate(this.getApplicationContext(), new long[]{100, 200, 100, 200}, 0);

//关闭震动
//        if (isVirating) {//防止多次关闭抛出异常，这里加个参数判断一下
//            isVirating = false;
//            VibrateUtil.virateCancle(this.getApplicationContext());
//        }
    }

    int current = 0;

    private void playVoice() {

        current++;
        textView.setText("当前:" + current);
        SoundPoolUtil.play(current);
        if (current == 7) {
            current = 0;
        }

    }

    private void showNotifi() {
        //高版本需要渠道
        if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            //只在Android O之上需要渠道
            NotificationChannel notificationChannel = new NotificationChannel("channelid1", "channelname", NotificationManager.IMPORTANCE_HIGH);
            //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道，通知才能正常弹出
            manager.createNotificationChannel(notificationChannel);
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, "channelid1");
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("通知标题")
                .setContentText("通知内容")
                .setAutoCancel(true);
        manager.notify(0x12, builder.build());

    }
}
